//
//  CTSetView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/28.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTSetView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "UIViewAdditions.h"
#import "CTSetView_Def.h"
#import "CTRequestUtil.h"
#import "CTUser.h"
#import "CTUserItem.h"
#import "CTTextManager.h"
#import "CTConfig.h"
#import "CTAppInfo.h"

#define CT_SETVIEW_BUTTON0 1000
#define CT_SETVIEW_BUTTON1 1001
#define CT_SETVIEW_BUTTON2 1002
#define CT_SETVIEW_BUTTON3 1003
#define CT_SETVIEW_BUTTON4 1004
#define CT_SETVIEW_BUTTON5 1005
//#define CT_SETVIEW_BUTTON6 1006

@interface CTSetView()
{
    UIView *topView;
    UILabel *cacheLabel;
    UIButton *exitBtn;
    UIView *headView;
    UIImageView * headImageView;
    UILabel *topLineLabel;
    UIView * listView;
    UIScrollView *setScrollView;
    UIButton *noticBtn;
    UIButton *joinBtn;
    
    BOOL noticationBool;
    
    CTUserItem * userItem;
}


@end

@implementation CTSetView

@synthesize delegate;

- (void)dealloc
{
    userItem = nil;
    delegate = nil;
    
    
    [headImageView removeFromSuperview];
    headImageView = nil;
    
    [headView removeFromSuperview];
    headView = nil;
    
    [topLineLabel removeFromSuperview];
    topLineLabel = nil;
    
    [listView removeFromSuperview];
    listView = nil;
    
    [setScrollView removeFromSuperview];
    setScrollView = nil;
    
    [topView removeFromSuperview];
    topView = nil;
    
    [cacheLabel removeFromSuperview];
    cacheLabel = nil;
    
    [exitBtn removeFromSuperview];
    exitBtn = nil;
    
    [noticBtn  removeFromSuperview];
    noticBtn = nil;
    
    [joinBtn removeFromSuperview];
    joinBtn = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_SET_HEAD object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTSetView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        userItem = [CTUser userInfo];
        
        self.frame = CGRectMake(0, 0,CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        [self createSetTitleView];
        [self createSetView];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPersonalHead:) name:CT_NOTI_SET_HEAD object:nil];
    }
    return self;
}
//创建标题
- (void)createSetTitleView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    topView.alpha =0.98;
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"setView_title_text" withValue:nil withTable:@"Language"];
    titleLabel.font = [UIFont systemFontOfSize:19.0f];
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLabel];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
}
//设置界面
- (void)createSetView
{
    setScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topView.bottom, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-topView.height)];
    setScrollView.showsHorizontalScrollIndicator = NO;
    setScrollView.contentSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, 420);
    [self addSubview:setScrollView];
    
    headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 100)];
    [setScrollView addSubview:headView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureActioin:)];
    [headView addGestureRecognizer:tapGesture];
    [self addSubview:topView];

    UIImageView *goImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-10-15, 42.5, 15, 15)];
    goImageView.image = [UIImage imageNamed:@"about_icon_go"];
    [headView addSubview:goImageView];
    
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 11.3, 77, 77)];
    headImageView.layer.masksToBounds = YES;
    headImageView.layer.cornerRadius  = 77/2.0f;
    [headView addSubview:headImageView];
    if ([[CTTextManager shareInstance] isBlankString:userItem.profile_image_url_thumb] == YES) {
        headImageView.image = [UIImage imageNamed:@"head"];
    }else{
        
        [CTRequestUtil requestImgWithUrl:userItem.profile_image_url_thumb withCompletionSuccessHandler:^(UIImage *img) {
            headImageView.image = img;
        } withFailedHandler:^(NSError *error) {
            
        }];
    }
    
    UILabel *headLabel = [[UILabel alloc]initWithFrame:CGRectMake(headImageView.right+10, 43, CT_HEADER_SCREEN_WIDTH-150, 20)];
    headLabel.font = [UIFont systemFontOfSize:15];
    headLabel.textAlignment = NSTextAlignmentLeft;
    headLabel.text = [CTAppLanguage localizedStringForKey:@"setView_head_Label" withValue:nil withTable:@"Language"];
    headLabel.textColor = CTABOUTUSVIEW_APPNAME_COLOR;
    [headView addSubview:headLabel];
    
    
    NSArray *headImageNameStr = [NSArray arrayWithObjects:@"tongzhikaiguan",@"qinglihuancun",@"about",@"pinglunyingyong",@"renzhenghuashi",@"yijianfankui", nil];
    NSArray *titlNameStr =[NSArray arrayWithObjects:[CTAppLanguage localizedStringForKey:@"setView_label_text1" withValue:nil withTable:@"Language"],[CTAppLanguage localizedStringForKey:@"setView_label_text2" withValue:nil withTable:@"Language"],[CTAppLanguage localizedStringForKey:@"setView_label_text4" withValue:nil withTable:@"Language"],[CTAppLanguage localizedStringForKey:@"setView_label_text5" withValue:nil withTable:@"Language"],[CTAppLanguage localizedStringForKey:@"setView_label_text6" withValue:nil withTable:@"Language"],[CTAppLanguage localizedStringForKey:@"setView_label_text7" withValue:nil withTable:@"Language"], nil];
    
    topLineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, headView.bottom, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topLineLabel.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [setScrollView addSubview:topLineLabel];
    listView = [[UIView alloc]initWithFrame:CGRectMake(0, topLineLabel.bottom, CT_HEADER_SCREEN_HEIGHT, 330)];
    [setScrollView addSubview:listView];
    for (int i = 0; i < 6; i++)
    {
        UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 13.5+i*45, 18, 18)];
        logoImageView.image = [UIImage imageNamed:headImageNameStr[i]];
        [listView addSubview:logoImageView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(logoImageView.right+10, i*45, CT_HEADER_SCREEN_WIDTH-70, 45)];
        if (i==1)
        {
            label.frame = CGRectMake(logoImageView.right+10, i*45, CT_HEADER_SCREEN_WIDTH-170, 45);
            cacheLabel = [[UILabel alloc]initWithFrame:CGRectMake(label.right, label.origin.y, 100, 45)];
            cacheLabel.textColor = SET_LABEL_TEXT_COLOR;
//            cacheLabel.text = [CTAppInfo appCacheCalculate];
            cacheLabel.textAlignment = NSTextAlignmentRight;
            cacheLabel.font = [UIFont fontWithName:nil size:15];
            [listView addSubview:cacheLabel];
        }
        label.text = titlNameStr[i];
        label.textColor = CTABOUTUSVIEW_APPNAME_COLOR;
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont fontWithName:nil size:15];
        [listView addSubview:label];
        
        if (i == 1)
        {
            UIImageView *arrowsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(cacheLabel.right, 15 +i*45, 15, 15)];
            arrowsImageView.image = [UIImage imageNamed:@"about_icon_go"];
            [listView addSubview:arrowsImageView];
        }
        else
        {
            UIImageView *arrowsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(label.right, 15 +i*45, 15, 15)];
            arrowsImageView.image = [UIImage imageNamed:@"about_icon_go"];
            [listView addSubview:arrowsImageView];
        }
        
        if (i == 0)
        {
            noticBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            noticBtn.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-55, 8+i*45, 45, 30);
            [noticBtn setBackgroundImage:[UIImage imageNamed:@"kaiguan_open"] forState:UIControlStateNormal];
//            [noticBtn addTarget:self action:@selector(buttonNotAction:) forControlEvents:UIControlEventTouchUpInside];
            [listView addSubview:noticBtn];
        }
        if (i == 5)
        {
            joinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            joinBtn.titleLabel.font = [UIFont systemFontOfSize:12];
            joinBtn.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-63, 8+i*45, 53, 30);
            [joinBtn setBackgroundImage:[UIImage imageNamed:@"join"] forState:UIControlStateNormal];
            [joinBtn setTitle:[CTAppLanguage localizedStringForKey:@"setView_joinBtn_title" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
//            [joinBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [listView addSubview:joinBtn];
        }
        if (i == 4)
        {
            UIButton * joinButton = [UIButton buttonWithType:UIButtonTypeCustom];
            joinButton.titleLabel.font  = [UIFont systemFontOfSize:12.0f];
            joinButton.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-63, 8+i*45, 53, 30);
            [joinButton setBackgroundImage:[UIImage imageNamed:@"join"] forState:UIControlStateNormal];
            [joinButton setTitle:[CTAppLanguage localizedStringForKey:@"setView_joinBtn_title" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
//            [joinButton addTarget:self action:@selector(addJoinBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [listView addSubview:joinButton];

        }
        //label
        if (i==5)
        {
            UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 45+i*45, CT_HEADER_SCREEN_WIDTH, 0.5)];
            bottomLabel.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
            [listView addSubview:bottomLabel];
        }
        else
        {
//            if (i != 0)
//            {
                UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 45+i*45, CT_HEADER_SCREEN_WIDTH-20, 0.5)];
                bottomLabel.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
                [listView addSubview:bottomLabel];
//            }
            
        }
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, i*45, CT_HEADER_SCREEN_WIDTH, 45);
        
//        if (i ==3 )
//        {
//            button.frame = CGRectMake(0, i*45, CT_HEADER_SCREEN_WIDTH-45, 45);
//        }
//        if (i == 5)
//        {
//            button.frame = CGRectMake(0, i*45, CT_HEADER_SCREEN_WIDTH-50, 45);
//        }
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000+i;
        [listView addSubview:button];
    }
    exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    exitBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-305)/2, 478-60, 305, 35);
    [exitBtn addTarget:self action:@selector(exitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [exitBtn setBackgroundImage:[UIImage imageNamed:@"signup_bg"] forState:UIControlStateNormal];
    [exitBtn setBackgroundImage:[UIImage imageNamed:@"signup_bg"] forState:UIControlStateHighlighted];
    [exitBtn setTitle:[CTAppLanguage localizedStringForKey:@"setView_exitButton_text" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [exitBtn setTitle:[CTAppLanguage localizedStringForKey:@"setView_exitButton_text" withValue:nil withTable:@"Language"] forState:UIControlStateHighlighted];
    [exitBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    [exitBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateHighlighted];
    [setScrollView addSubview:exitBtn];
    
}
//设置个人资料
- (void)tapGestureActioin:(UITapGestureRecognizer *) sender
{
    if ([self.delegate respondsToSelector:@selector(setPersonalProfileBtnAction:)])
    {
        [self.delegate setPersonalProfileBtnAction:self];
    }
}
//通知开关
//- (void)buttonNotAction:(UIButton *) sender
//{
//    if (noticationBool)
//    {
//        [sender setBackgroundImage:[UIImage imageNamed:@"kaiguan_close"] forState:UIControlStateNormal];
//        noticationBool = NO;
//    }
//    else
//    {
//        [sender setBackgroundImage:[UIImage imageNamed:@"kaiguan_open"] forState:UIControlStateNormal];
//        noticationBool = YES;
//    }
//    if ([self.delegate respondsToSelector:@selector(setViewNoticationBtnAction:noticationBool:)])
//    {
//        [self.delegate setViewNoticationBtnAction:self noticationBool:noticationBool];
//    }
//}
//退出按钮
- (void)exitButtonAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(setViewExitBtnAction:)])
    {
        [self.delegate setViewExitBtnAction:self];
    }
}
//buttonAction
- (void)buttonAction:(UIButton *) sender
{
    switch (sender.tag)
    {
        case CT_SETVIEW_BUTTON0:
                if (noticationBool == YES)
                {
                    [noticBtn setBackgroundImage:[UIImage imageNamed:@"kaiguan_close"] forState:UIControlStateNormal];
                    noticationBool = NO;
                }
                else
                {
                    [noticBtn setBackgroundImage:[UIImage imageNamed:@"kaiguan_open"] forState:UIControlStateNormal];
                    noticationBool = YES;
                }
                if ([self.delegate respondsToSelector:@selector(setViewNoticationBtnAction:noticationBool:)])
                {
                    [self.delegate setViewNoticationBtnAction:self noticationBool:noticationBool];
                }
            break;
        case CT_SETVIEW_BUTTON1:
            if ([self.delegate respondsToSelector:@selector(setViewClearCacheBtnAction:)])
            {
                [self.delegate setViewClearCacheBtnAction:self];
            }
            break;
        case CT_SETVIEW_BUTTON2:
            if ([self.delegate respondsToSelector:@selector(setViewCommentBtnAction:)])
            {
                [self.delegate setViewCommentBtnAction:self];
            }
            break;
        case CT_SETVIEW_BUTTON3:
            if ([self.delegate respondsToSelector:@selector(setViewAboutBtnAction:)])
            {
                [self.delegate setViewAboutBtnAction:self];
            }
            break;
        case CT_SETVIEW_BUTTON4:
            if ([self.delegate respondsToSelector:@selector(setViewCertificationBtnAction:)])
            {
                [self.delegate setViewCertificationBtnAction:self];
//                [self joinGroup:@"hello" key:@"hello"];
            }
            break;
        case CT_SETVIEW_BUTTON5:
            if ([self.delegate respondsToSelector:@selector(setViewOpinionBtnAction:)])
            {
                [self.delegate setViewOpinionBtnAction:self];
//                [self joinGroup1:@"hellow" key:@"hello"];
            }
            break;
                default:
            break;
    }
}

//返回按钮事件
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(setViewBackButton:)])
    {
        [self.delegate setViewBackButton:self];
    }
}
#pragma mark - setter action -

-(void)setCache:(NSString *)cacheText
{
    cacheLabel.text = cacheText;
}
-(void) setNotication:(BOOL)notication
{
    noticationBool = notication;
    if (notication)
    {
        [noticBtn setBackgroundImage:[UIImage imageNamed:@"kaiguan_close"] forState:UIControlStateNormal];
    }
    else
    {
        [noticBtn setBackgroundImage:[UIImage imageNamed:@"kaiguan_open"] forState:UIControlStateNormal];
    }
}
-(void)setIsLogin:(BOOL)isLogin
{
    if (isLogin)
    {
        exitBtn.hidden = NO;
    }
    else
    {
        setScrollView.contentSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, 225);
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, -50, CT_HEADER_SCREEN_WIDTH, 70)];
//        view.backgroundColor = [UIColor redColor];
//        [setScrollView addSubview:view];
        setScrollView.frame = CGRectMake(0, -10.5, CT_HEADER_SCREEN_WIDTH, setScrollView.frame.size.height);
        exitBtn.hidden = YES;
        headView.hidden = YES;
//        topLineLabel.frame = CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 0.5);
        topLineLabel.hidden = YES;
        listView.frame = CGRectMake(0, topView.bottom-55, CT_HEADER_SCREEN_WIDTH, 330);

    }
    
}

#pragma mark - noti -

-(void)setPersonalHead:(id)sender
{
    NSNotification * noti = (NSNotification *)sender;
    UIImage * headImg = (UIImage *)noti.object;
    
    headImageView.image = headImg;
}

@end
